<?php
/**
 * Created by PhpStorm.
 * User: ytlmi
 * Date: 2017/11/9
 * Time: 9:51
 */

namespace app\common\commonClass;

class PageTools
{

    static function nav(array $arr_nav, $active='',$head='DNA LAB')
    {
        $user = \think\Session::get('name');

        $nav_start = '
        <nav class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="#">'.$head.'</a>
            </div>
            <div  id="navbar" class="navbar-collapse collapse">
                <ul class="nav navbar-nav">';
        $nav_end = '
                </ul>
                <ul class="nav navbar-nav navbar-right">
                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Hello, '.$user.'<span class="caret"></span></a>
                    <ul class="dropdown-menu">
                        <li><a onclick="$.myExit()">Exit</a></li>
                    </ul>
            </div>
        </div>
        </nav>';

        $nav_body = '';

        foreach ($arr_nav as $nav) {
            if (is_array($nav)) {
                $nav_body .= '<li class="dropdown">
                <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">' . $nav['title'] . '<span class="caret"></span></a>
                <ul class="dropdown-menu">';
                foreach ($nav['content'] as $item) {
                    $arr_str = preg_split('/,/', $item);
                    if ($active == $nav['title'] .'.'. $arr_str[0]) {
                        $str_action = 'class="active"';
                        $link = '#';
                    } else {
                        $str_action = '';
                        $link = $arr_str[1];
                    }
                    $nav_body .= '<li ' . $str_action . '><a href="' . $link . '">' . $arr_str[0] . '</a></li>';
                }
                $nav_body .= '</ul></li>';
            } else {
                $arr_str = preg_split('/,/', $nav);
                if ($active == $arr_str[0]) {
                    $str_action = 'class="active"';
                    $link = '#';
                } else {
                    $str_action = '';
                    $link = $arr_str[1];
                }
                $nav_body .= '<li ' . $str_action . '><a href="' . $link . '">' . $arr_str[0] . '</a></li>';
            }
            $nav_body.='<li class="divider"></li>';

        }
        return $nav_start.$nav_body.$nav_end;
    }

    static function footer()
    {

    }


}